package com.sto.stoapp.db.manager;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.constant.DataDictConstant;
import com.sto.stoapp.db.constant.GoodsTypeConstant;
import com.sto.stoapp.db.constant.StoBaseConstant;
import com.sto.stoapp.http.response.download.info.GoodsTypeInfo;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;
/**
 * 物品类别管理类
 * @author cjb
 *
 */
public class GoodsTypeManager extends StoDataBase implements
		DownLoadDao<GoodsTypeInfo> {
	static GoodsTypeManager mgr;

	private GoodsTypeManager(Context context) {
		super(context);

	}

	public static GoodsTypeManager getInstance(Context c) {
		if (mgr == null)
			mgr = new GoodsTypeManager(c);
		return mgr;
	}

	public boolean insert(GoodsTypeInfo entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				ContentValues values = new ContentValues();
				values.put(GoodsTypeConstant.KEY, entity.key);
				values.put(GoodsTypeConstant.VALUE, entity.value);
				values.put(GoodsTypeConstant.TYPE, entity.type);
				values.put(GoodsTypeConstant.BACKUP1, entity.backup1);
				values.put(GoodsTypeConstant.VERSION, entity.version);
				values.put(GoodsTypeConstant.ID, entity.id);
				// values.put(STATUS, entity.getStatus());
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.insert(GoodsTypeConstant.GOODS_TYPE_TABLE, null, values);
				if (res > 0) {
					Log.d("插入一条物品类别信息", "成功");
				} else {
					Log.d("插入一条物品类别信息", "失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insert", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	@Override
	public String getMaxVersion() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			String version = "0";
			Cursor c = null;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.READ_DATABASE);
				c = db.rawQuery(getMaxVersionSql(GOODS_TYPE_TABLE), null);
				if (c != null && c.moveToNext()) {
					version = c.getString(c
							.getColumnIndex(StoBaseConstant.VERSION));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeCursor(c);
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return version;
		}
	}

	@Override
	public boolean replace(ContentValues values) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long result = 0;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				result = db.replace(GOODS_TYPE_TABLE, null, values);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result > 0;
		}
	}

	@Override
	public void deleteDataByStatus(String status) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {

			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				db.delete(GOODS_TYPE_TABLE, STATUS + "=?",
						new String[] { status });
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}
	}

	@Override
	public ContentValues buildContentValues(GoodsTypeInfo entity) {
		ContentValues values = new ContentValues();
		values.put(GoodsTypeConstant.KEY, entity.key);
		values.put(GoodsTypeConstant.VALUE, entity.value);
		values.put(GoodsTypeConstant.TYPE, entity.type);
		values.put(GoodsTypeConstant.BACKUP1, entity.backup1);
		values.put(GoodsTypeConstant.VERSION, entity.version);
		values.put(DataDictConstant.ID, entity.id);
		values.put(STATUS, entity.status);
		return values;
	}

	/**
	 * 插入多数据
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void insertOrReplace(List<GoodsTypeInfo> datas) {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
			try {
				db.beginTransaction();
				for (GoodsTypeInfo info : datas) {
					long res = -1;
					ContentValues values = buildContentValues(info);
//					res = db.insert(GOODS_TYPE_TABLE, null, values);
//					if (res > 0) {
//						Log.d("插入一条物品类别信息", "成功");
//					} else {
						if (replace(values)) {
							Log.d("插入一条物品类别信息", "成功");
						} else {
							Log.d("插入一条物品类别信息", "失败");
	//					}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insertList", e.getMessage());
			} finally {
				datas.clear();
				db.setTransactionSuccessful();
				db.endTransaction();
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
		}
	}
	@Override
	public void deleteAll() {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.delete(GOODS_TYPE_TABLE, null, null);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("deleteAll", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}

	}
}
